package com.goldze.mvvmhabit.ui.rvgroup;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.goldze.mvvmhabit.BR;
import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.databinding.FragmentExpandRvBinding;
import com.goldze.mvvmhabit.widght.expand.ExpandedRvAdapter;
import com.h6ah4i.android.widget.advrecyclerview.animator.GeneralItemAnimator;
import com.h6ah4i.android.widget.advrecyclerview.animator.RefactoredTranslatetItemAnimator;
import com.h6ah4i.android.widget.advrecyclerview.expandable.RecyclerViewExpandableItemManager;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * create om  2020/11/16.
 * Created by  gaoxuge
 * email android_gaoxuge@163.com
 * 功能描述
 */
public class ExpandRvFragment extends BaseFragment<FragmentExpandRvBinding,ExPandRvViewModel> {
    private RecyclerView.LayoutManager mLayoutManager;
    private RecyclerView.Adapter mWrappedAdapter;
    private RecyclerViewExpandableItemManager mRecyclerViewExpandableItemManager;
    @Override
    public int initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return R.layout.fragment_expand_rv;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        mLayoutManager = new LinearLayoutManager(requireContext());

        mRecyclerViewExpandableItemManager = new RecyclerViewExpandableItemManager(null);
        final ExpandedRvAdapter myItemAdapter = new ExpandedRvAdapter(mRecyclerViewExpandableItemManager);
        ///////////////////////////
        //设置数据
        myItemAdapter.setItems(viewModel.list);
        myItemAdapter.setItemBinding(viewModel.groupItemBinding, viewModel.childItemBinding);
        //////////////////////////
        mWrappedAdapter = mRecyclerViewExpandableItemManager.createWrappedAdapter(myItemAdapter);


        final GeneralItemAnimator animator = new RefactoredTranslatetItemAnimator();
        animator.setSupportsChangeAnimations(false);
        animator.setAddDuration(200);
        binding.rvExpand.setLayoutManager(mLayoutManager);
        binding.rvExpand.setAdapter(mWrappedAdapter);  // requires *wrapped* adapter
        binding.rvExpand.setItemAnimator(animator);
        binding.rvExpand.setHasFixedSize(false);

        mRecyclerViewExpandableItemManager.attachRecyclerView(binding.rvExpand);

        viewModel.initData();
    }

    @Override
    public void initViewObservable() {

    }
}
